<template>
	<view>
		<view class="goods-list">
			<view v-for="(item, i) in goodsList" :key="i" @click="gotoDetail(item)">
				<my-goods :goods="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{
					query:'',
					cid: '',
					// 页码
					pagenum: 1 ,
					// 每页显示多少数据
					pagesize: 10
				},
				goodsList:[],
				total:0,
				isLoading:false
			}
		},
		onLoad(options) {
			// 获取传递过来的参数
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			
			this.getGoodsList()
		},
		onReachBottom() {
			// 判断数据是否加载完毕，加载完毕则不再请求
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
			// 判断是否正在发起请求
			if(this.isLoading) return
			
			// 上拉触底
			this.queryObj.pagenum += 1
			this.getGoodsList()
		},
		onPullDownRefresh() {
			// 下拉刷新
			// 重置关键数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isLoading = false
			this.goodsList = []
			// 重新发起数据请求
			this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		methods: {
			async getGoodsList(cb){
				// 打开节流阀
				this.isLoading = true
				// 发起请求
				const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
				// 关闭节流阀
				this.isLoading = false
				// 调用回调函数
				cb && cb()
				if(res.meta.status !== 200) return uni.$showMsg
				
				this.goodsList = [...this.goodsList,...res.message.goods]
				this.total = res.message.total
				console.log(this.goodsList)
				console.log(this.total)
			},
			// 点击跳转商品详情页
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?good_id=' + item.goods_id
				})
			}
		}
	}
</script>

<style lang="scss">
	
</style>
